package com.example.secondhandmall.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.example.secondhandmall.R;
import com.example.secondhandmall.adapter.MyPagerAdapter;
import com.example.secondhandmall.entity.TabEntity;
import com.example.secondhandmall.fragment.BlankFragment;
import com.example.secondhandmall.fragment.HomeFragment;
import com.example.secondhandmall.fragment.MineFragment;
import com.example.secondhandmall.fragment.PutFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

public class HomeActivity extends BaseActivity {

    private String[] mTitles = {"首页", "上架商品", "我的"/*,"测试"*/};
    private int[] mIconUnselectIds = {
            R.mipmap.home, R.mipmap.put,
            R.mipmap.mine/*, R.mipmap.put,*/};
    private int[] mIconSelectIds = {
            R.mipmap.home_selected, R.mipmap.put_selected,
            R.mipmap.mine_selected/*, R.mipmap.put,*/};

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;

    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.viewpager);
        commonTabLayout = findViewById(R.id.commonTabLayout);
    }

    @Override
    protected void initData() {
        //fragment添加到viewPage中
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(PutFragment.newInstance());
        mFragments.add(MineFragment.newInstance());
//        mFragments.add(BlankFragment.newInstance("",""));
        //添加按钮实体
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        //将实体集合绑定到组件
        commonTabLayout.setTabData(mTabEntities);
        //Tab切换点击事件
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                if (position == 1) {
//                    navigateTo(PutActivity.class);
//                } else {
//                    viewPager.setCurrentItem(position);
//                }
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        //预加载Fragment
        viewPager.setOffscreenPageLimit(mFragments.size());
        //页面切换，tab也切换
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                if (position == 1) {
//                    navigateTo(PutActivity.class);
//                } else {
//                    commonTabLayout.setCurrentTab(position);
//                }
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
    }
}